package com.mobimagic.lockscreen.view;

import android.graphics.Canvas;
import android.graphics.Paint;

/**
 *
 * 处理...的逻辑
 *
 * @Description
 */
public class SimpleStaticLayout {

    private static final String ELLIPSIZE_END = "...";
    private Paint paint;
    private String text;
    private String targetText;
    /**
     * 期望宽度
     */
    private float expectWidth;
    private float absloteWidth;

    public SimpleStaticLayout() {

    }

    public SimpleStaticLayout(Paint paint, String text, float expectWidth) {
        super();
        this.paint = paint;
        this.text = text;
        this.expectWidth = expectWidth;
        absloteWidth = paint.measureText(text);
        if (absloteWidth > expectWidth) {
            if (text.length() > 0) {
                for (int i = text.length() - 1; i >= 0; i--) {
                    String sb = text.substring(0, i);
                    String temp = sb + ELLIPSIZE_END;
                    float measureWidth = paint.measureText(temp);
                    if (measureWidth <= expectWidth) {
                        targetText = temp;
                        absloteWidth = measureWidth;
                        break;
                    }
                }

            }
        } else {
            targetText = text;
        }

    }

    public void draw(Canvas canvas, float x, float y) {
        canvas.drawText(targetText, x, y, paint);
    }
}
